//
//  TopicViewController.m
//  Lianxi2
//
//  Created by lanou on 16/7/2.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "TopicViewController.h"

@interface TopicViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UITableView *hotTableView;
@property (nonatomic,strong)UIScrollView *scroll;
@property (nonatomic,strong)UISegmentedControl *segment;//分段控制器

@property (nonatomic,strong)NSMutableArray *topicNewArray;
@property (nonatomic,strong)NSMutableArray *topicHotArray;
@end

@implementation TopicViewController
#pragma mark--------懒加载--------
- (UISegmentedControl *)segment{
    if (!_segment) {
        
        _segment = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
        [_segment insertSegmentWithTitle:@"最热" atIndex:0 animated:YES];
        [_segment insertSegmentWithTitle:@"最新" atIndex:1 animated:YES];
        [_segment addTarget:self action:@selector(clickSegmentedControll:) forControlEvents:UIControlEventValueChanged ];
    }
    return _segment;
}
- (UIScrollView *)scroll{
    if (!_scroll) {
        _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight-64)];
        _scroll.contentSize = CGSizeMake(kWidth*2, kHeight-64);
        _scroll.bounces = NO;
        _scroll.bouncesZoom = NO;
        _scroll.pagingEnabled=  YES;
        _scroll.delegate = self;
        
    }
    return _scroll;
}
- (UITableView *)hotTableView{
    if (!_hotTableView) {
        _hotTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-64) style:UITableViewStylePlain];
        _hotTableView.dataSource = self;
        _hotTableView.delegate = self;
        [self.scroll addSubview:_hotTableView];
    }
    return _hotTableView;
}
- (NSMutableArray *)topicHotArray{
    if (!_topicHotArray) {
        _topicHotArray = [NSMutableArray array];
    }
    return _topicHotArray;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(kWidth, 0, kWidth, kHeight-64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self.scroll addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)topicNewArray{
    if (!_topicNewArray) {
        _topicNewArray = [NSMutableArray array];
    }
    return _topicNewArray;
}
#pragma mark--------SegmentControll点击事件--------
- (void)clickSegmentedControll:(UISegmentedControl *)sender{
    
    if (sender.selectedSegmentIndex == 0) {
        [self.scroll setContentOffset:CGPointZero animated:YES];
    }else{
        [self.scroll setContentOffset:CGPointMake(kWidth, 0) animated:YES];
    }
}
#pragma mark--------界面生命周期--------
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.scroll];
    
    //添加标签视图控制器
    self.navigationItem.titleView = self.segment;
    self.segment.selectedSegmentIndex = 0;
    
    [RequestManager fetchDataWithURL:KTopicUrl parameter:@{@"sort":@"addtime"} metod:@"POST" compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            TopicModel *model = [[TopicModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.topicNewArray addObject:model];
        }
        
        
        NSLog(@"%@",bigDic);
        
    } updateUI:^{
        
        
        [self.tableView reloadData];
    }];
    
    [RequestManager fetchDataWithURL:KTopicUrl parameter:@{} metod:@"GET" compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            TopicModel *model = [[TopicModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.topicHotArray addObject:model];
        }
        
        NSLog(@"%@",bigDic);
    } updateUI:^{
        
        [self.hotTableView reloadData];
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"TopicModel_cell"];
    [self.hotTableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"TopicModel_cell"];
    
}
#pragma mark--------UITableViewDelegate--------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.hotTableView) {
        return self.topicHotArray.count;
    }else{
        return self.topicNewArray.count;
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.hotTableView) {
        TopicModel *model = self.topicHotArray[indexPath.row];
        TopicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicModel_cell" forIndexPath:indexPath];
        [cell setCellWithModel:model];
        return cell;
    }else{
        TopicModel *model = self.topicNewArray[indexPath.row];
        TopicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicModel_cell" forIndexPath:indexPath];
        [cell setCellWithModel:model];
        return cell;
    }
    
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 205;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
